<?php
namespace app\mall\controller;

use app\mall\logic\loginLogic;
use app\mall\logic\userLogic;
use think\admin\Controller;
use think\admin\service\CaptchaService;

class Login extends Controller {

    /**
     * 生成验证码
     */
    public function captcha()
    {
        $input = $this->_vali(['type.require'  => '验证码类型不能为空!']);
        $image = CaptchaService::instance()->initialize();
        $captcha = ['image' => $image->getData(), 'captcha_id' => $image->getUniqid()];

        $captchaKey = "captcha-{$input['type']}-{$captcha['captcha_id']}";
        cache($captchaKey, $image->getCode(), ['expire' => 60]);

        $this->success('生成验证码成功', $captcha);
    }

    /**
     * 发送手机短信
     *
     * @return void
     */
    public function phone_sms()
    {
        $phone = request()->param('phone', '');
        $logic = new loginLogic();
        $res   = $logic::phone_sms($phone);
        if ($res['code'] == 200) {
            $this->success($res['msg']);
        }
        $this->error($res['msg'], '', $res['code']);
    }

    /**
     * @return void
     */
    public function login()
    {
        $phone       = request()->param('phone', '');
        $password    = request()->param('password', '');
        $smscode     = request()->param('sms_code', '');
//        $captchaCode = request()->param('captcha_code', '');
//        $captchaId   = request()->param('captcha_id', '');

//        $captchaKey = "captcha-login-{$captchaId}";
//        $result = cache($captchaKey);
//        if ($result !== strtoupper($captchaCode)) {
//            cache($captchaKey, NULL);
//            return $this->error("图形验证码异常！");
//        }

        $logic = new loginLogic();
        $res = $logic->login($phone, $password, $smscode);
        if ($res['code'] == 200) {
            return $this->success($res['msg'], $res['user_data']);
        }

        return $this->error($res['msg'], $res['user_data'], $res['code']);
    }

    /**
     * 手机号注册
     *
     * @return void
     * @throws \think\admin\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function phone_register()
    {
        $phone       = request()->param('phone', '');
        $password    = request()->param('password', '');
        $re_password = request()->param('re_password', '');
        $smscode     = request()->param('sms_code', 0);
        $captchaCode = request()->param('captcha_code', '');
        $captchaId   = request()->param('captcha_id', '');
        $p_phone     = request()->param('p_phone', '');

        $captchaKey = "captcha-register-{$captchaId}";
        $result = cache($captchaKey);
        if ($result !== strtoupper($captchaCode)) {
            return $this->error("图形验证码异常！");
        }

        $logic = new loginLogic();
        $res   = $logic->phone_register($phone, $password, $re_password, $smscode, $p_phone);
        if ($res['code'] == 200) {
            $this->success($res['msg']);
        }
        $this->error($res['msg'], '', $res['code']);
    }

    /**
     * @return void|null
     */
    public function retrieve()
    {
        $phone       = request()->param('phone', '');
        $password    = request()->param('password', '');
        $re_password = request()->param('re_password', '');
        $smscode     = request()->param('sms_code', 0);
        $captchaCode = request()->param('captcha_code', '');
        $captchaId   = request()->param('captcha_id', '');

        $captchaKey = "captcha-retrieve-{$captchaId}";
        $result = cache($captchaKey);
        if ($result !== strtoupper($captchaCode)) {
            cache($captchaKey, NULL);
            return $this->error("图形验证码异常！");
        }

        $logic = new userLogic();
        $result = $logic->reset_password(['phone' => $phone], $password, $re_password, $smscode);
        if ($result['code'] == 200) {
            $this->success($result['msg']);
        }
        $this->error($result['msg'], '', $result['code']);
    }
}